package com.jiuzhou.surery.security;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.jiuzhou.surery.beans.User;

public class SurveyUserDetails implements UserDetails {
    private final User user;
    private  Set<GrantedAuthority> authorities;

    public SurveyUserDetails(User user, Collection<? extends GrantedAuthority> authorities) {
        this.user = user;
        SortedSet<GrantedAuthority> sortedSet = new TreeSet<>((a,b) -> a.getAuthority().compareTo(b.getAuthority()));
        sortedSet.addAll(authorities);
        
        this.authorities = sortedSet;
    }
    public SurveyUserDetails(String username,String password,boolean enabled,String firecar,String authorities) {
        this.user = new User(null,username,password,enabled,firecar,new Date());
        this.authorities = Set.of(new SimpleGrantedAuthority(authorities));
       
    }
   
    public User getUser() {
        return user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }
}
